/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2007 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

extern "C" {
#include <stdint.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ysip2.h"
}

#include "../ysip2.h"

#define YSIP2_CCTC(str) (char*)(str)


#define REGION_STRUCT_TO_ARRAY(array, region) \
	bzero(_g_convert_buf, sizeof(_g_convert_buf)); \
	snprintf(_g_convert_buf, sizeof(_g_convert_buf), "%llu", (region).id); \
	add_assoc_stringl((array), YSIP2_CCTC("id"), _g_convert_buf, strlen(_g_convert_buf), 1); \
	bzero(_g_convert_buf, sizeof(_g_convert_buf)); \
	snprintf(_g_convert_buf, sizeof(_g_convert_buf), "%llu", (region).parent_id); \
	add_assoc_stringl((array), YSIP2_CCTC("parent_id"),_g_convert_buf, strlen(_g_convert_buf),1); \
	add_assoc_long((array), YSIP2_CCTC("level"), (region).level); \
	add_assoc_stringl((array), YSIP2_CCTC("name") , (region).name, strlen((region).name), 1);

#ifdef YSIP2_IP_DATA_ALL
#define REGION_EX_STRUCT_TO_ARRAY(array, region) \
	bzero(_g_convert_buf, sizeof(_g_convert_buf)); \
	snprintf(_g_convert_buf, sizeof(_g_convert_buf), "%llu", (region).id); \
	add_assoc_stringl((array), YSIP2_CCTC("id"), _g_convert_buf, strlen(_g_convert_buf), 1); \
	bzero(_g_convert_buf, sizeof(_g_convert_buf)); \
	snprintf(_g_convert_buf, sizeof(_g_convert_buf), "%llu", (region).parent_id); \
	add_assoc_stringl((array), YSIP2_CCTC("parent_id"),_g_convert_buf, strlen(_g_convert_buf),1); \
	add_assoc_long((array),  YSIP2_CCTC("level"), (region).level); \
	add_assoc_stringl((array),  YSIP2_CCTC("name") , (region).name, strlen((region).name), 1); \
	add_assoc_stringl((array), YSIP2_CCTC("detail"), YSIP2_CCTC((region).detail.c_str()), (region).detail.length(), 1);
#endif

using namespace std;
/* If you declare any globals in php_ysip2.h uncomment this: */
ZEND_DECLARE_MODULE_GLOBALS(ysip2)

/* True global resources - no need for thread safety here */
static int le_ysip2;

/* {{{ ysip2_functions[]
 *
 * Every user visible function must have an entry in ysip2_functions[].
 */
zend_function_entry ysip2_functions[] = {
	PHP_FE(confirm_ysip2_compiled,	NULL)		/* For testing, remove later. */
	PHP_FE(ysip_get_ip_info,	NULL)
	PHP_FE(ysip_get_ip_info_ex,	NULL)
	PHP_FE(ysip_get_region_info,	NULL)
	PHP_FE(ysip_get_region_childs,	NULL)
	PHP_FE(ysip_get_region_parent,	NULL)
	PHP_FE(ysip_get_region_parents,	NULL)
	PHP_FE(ysip_get_stat, NULL)
	{NULL, NULL, NULL}	/* Must be the last line in ysip2_functions[] */
};
/* }}} */

/* {{{ ysip2_module_entry
 */
zend_module_entry ysip2_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
	STANDARD_MODULE_HEADER,
#endif
	"ysip2",
	ysip2_functions,
	PHP_MINIT(ysip2),
	PHP_MSHUTDOWN(ysip2),
	NULL, //PHP_RINIT(ysip2),	/* Replace with NULL if there's nothing to do at request start */
	NULL, //PHP_RSHUTDOWN(ysip2),	/* Replace with NULL if there's nothing to do at request end */
	PHP_MINFO(ysip2),
#if ZEND_MODULE_API_NO >= 20010901
	"2.0", /* Replace with version number for your extension */
#endif
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_YSIP2
ZEND_GET_MODULE(ysip2)
#endif

/* {{{ PHP_INI
 */
/* Remove comments and fill if you need to have entries in php.ini  */
PHP_INI_BEGIN()
//    STD_PHP_INI_ENTRY("ysip2.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_ysip2_globals, ysip2_globals)
    STD_PHP_INI_ENTRY("ysip2.root", NULL, PHP_INI_ALL, OnUpdateString, root, zend_ysip2_globals, ysip2_globals)
PHP_INI_END()
/* }}} */

/* {{{ php_ysip2_init_globals
 */
/* Uncomment this function if you have INI entries */
static void php_ysip2_init_globals(zend_ysip2_globals *ysip2_globals)
{
//	ysip2_globals->global_value = 0;
	ysip2_globals->root = NULL;
}
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(ysip2)
{
	ZEND_INIT_MODULE_GLOBALS(ysip2, php_ysip2_init_globals, NULL);
	/* If you have INI entries, uncomment these lines */
	REGISTER_INI_ENTRIES();
	if(YSIP2_G(root) == NULL) {
		ysipInit();
	} else {
		ysipInit(YSIP2_G(root));
	}
	REGISTER_LONG_CONSTANT("YSIP_LEVEL_COUNTRY", YSIP_LEVEL_COUNTRY, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("YSIP_LEVEL_PROVINCE", YSIP_LEVEL_PROVINCE, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("YSIP_LEVEL_AREA", YSIP_LEVEL_AREA, CONST_CS | CONST_PERSISTENT);
	REGISTER_LONG_CONSTANT("YSIP_LEVEL_CITY", YSIP_LEVEL_CITY, CONST_CS | CONST_PERSISTENT);
	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(ysip2)
{
	/* uncomment this line if you have INI entries */
	ysipDestory();
#ifdef ZTS
	ts_free_id(ysip2_globals_id);
#endif
	UNREGISTER_INI_ENTRIES();
	return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
/*
PHP_RINIT_FUNCTION(ysip2)
{
	return SUCCESS;
}
*/
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
/*
PHP_RSHUTDOWN_FUNCTION(ysip2)
{
	return SUCCESS;
}
*/
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(ysip2)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "ysip2 support", "enabled");
	php_info_print_table_end();

	/* Remove comments if you have entries in php.ini  */
	DISPLAY_INI_ENTRIES();
}
/* }}} */


/* Remove the following function when you have succesfully modified config.m4
   so that your module can be compiled into PHP, it exists only for testing
   purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_ysip2_compiled(string arg)
   Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_ysip2_compiled)
{
	char *arg = NULL;
	int arg_len, len;
	char *strg;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, YSIP2_CCTC("s"), &arg, &arg_len) == FAILURE) {
		return;
	}

	len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ysip2", arg);
	RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and 
   unfold functions in source code. See the corresponding marks just before 
   function definition, where the functions purpose is also documented. Please 
   follow this convention for the convenience of others editing your code.
*/

/* {{{ proto array ysip_get_ip_info(string ip, int type )
   ; */
PHP_FUNCTION(ysip_get_ip_info)
{
	char *ip = NULL;
	int argc = ZEND_NUM_ARGS();
	int ip_len;

	if (zend_parse_parameters(argc TSRMLS_CC, YSIP2_CCTC("s"), &ip, &ip_len) == FAILURE) {
		RETURN_FALSE;
	}
	YSIP2_REGION_INFO regionInfo;

char _g_convert_buf[20];
	if(ysipGetIpInfo(ip, regionInfo)) {
		array_init(return_value);
		REGION_STRUCT_TO_ARRAY(return_value, regionInfo);
		return;
	}
	RETURN_FALSE;
}
/* }}} */


/* {{{ proto array ysip_get_ip_info_ex(string ip, int type )
   ; */
PHP_FUNCTION(ysip_get_ip_info_ex)
{
	char *ip = NULL;
	int argc = ZEND_NUM_ARGS();
	int ip_len;
	int type;

	if (zend_parse_parameters(argc TSRMLS_CC, YSIP2_CCTC("s"), &ip, &ip_len) == FAILURE) {
		RETURN_FALSE;
	}
	YSIP2_REGION_INFO_EX regionInfo;

char _g_convert_buf[20];
	if(ysipGetIpInfo(ip, regionInfo)) {
		array_init(return_value);
		REGION_EX_STRUCT_TO_ARRAY(return_value, regionInfo);
		return;
	}
	RETURN_FALSE;
}
/* }}} */
/* {{{ proto array ysip_get_region_info(string id)
   ; */
PHP_FUNCTION(ysip_get_region_info)
{
	int argc = ZEND_NUM_ARGS();
	char * id = NULL;
	int id_len;

	if (zend_parse_parameters(argc TSRMLS_CC, YSIP2_CCTC("s"), &id, &id_len) == FAILURE) 
		return;

char _g_convert_buf[20];
	int64_t n = strtoll(id, NULL, 10);
	YSIP2_REGION_INFO regionInfo;
	if(ysipGetRegionInfo(n, regionInfo)) {
		array_init(return_value);
		REGION_STRUCT_TO_ARRAY(return_value, regionInfo);
		return;
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto array ysip_get_region_childs(string id)
   ; */
PHP_FUNCTION(ysip_get_region_childs)
{
	int argc = ZEND_NUM_ARGS();
	char * id = NULL;
	int id_len = 0;
	long is = 1; // include self

	if (zend_parse_parameters(argc TSRMLS_CC, YSIP2_CCTC("s|l"), &id, &id_len, &is) == FAILURE)  {
		RETURN_FALSE;
	}

	char _g_convert_buf[20];
	int64_t n = strtoll(id, NULL, 10);
	vector<YSIP2_REGION_INFO> vec;
	if(ysipGetRegionChilds(n, vec, is != 0)) {
		array_init(return_value);
		zval *z;
		vector<YSIP2_REGION_INFO>::iterator iter;
		for(iter = vec.begin(); iter < vec.end(); iter ++) {
			MAKE_STD_ZVAL(z);
			array_init(z);
			REGION_STRUCT_TO_ARRAY(z, *iter);
			add_next_index_zval(return_value, z);			
		}
		return;
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto array ysip_get_region_parent(int id)
   ; */
PHP_FUNCTION(ysip_get_region_parent)
{
	int argc = ZEND_NUM_ARGS();
	char * id = NULL;
	int id_len = 0;

	if (zend_parse_parameters(argc TSRMLS_CC, YSIP2_CCTC("s"), &id, &id_len) == FAILURE) 
		return;

char _g_convert_buf[20];
	int64_t n = strtoll(id, NULL, 10);
	YSIP2_REGION_INFO regionInfo;
	if(ysipGetRegionParent(n, regionInfo)) {
		array_init(return_value);
		REGION_STRUCT_TO_ARRAY(return_value, regionInfo);
		return;
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto array ysip_get_region_parents(string id)
   ; */
PHP_FUNCTION(ysip_get_region_parents)
{
	int argc = ZEND_NUM_ARGS();
	char * id = NULL;
	int id_len = 0;
	long is = 1; //include self
	long level = YSIP_MIN_LEVEL;

	if (zend_parse_parameters(argc TSRMLS_CC, "s|ll", &id, &id_len, &is, &level) == FAILURE) {
		RETURN_FALSE;
	}

	char _g_convert_buf[20];
	int64_t n = strtoll(id, NULL, 10);
	vector<YSIP2_REGION_INFO> vec;
	if(ysipGetRegionParents(n, vec, is != 0, level)) {
		array_init(return_value);
		zval *z;
		vector<YSIP2_REGION_INFO>::iterator iter;
		for(iter = vec.begin(); iter < vec.end(); iter ++) {
			MAKE_STD_ZVAL(z);
			array_init(z);
			REGION_STRUCT_TO_ARRAY(z, *iter);
			add_next_index_zval(return_value, z);			
		}
		return;
	}
	RETURN_FALSE;
}
/* }}} */

/* {{{ proto array ysip_get_stat(int type)
   ; */
PHP_FUNCTION(ysip_get_stat)
{

	int regionStat = ysipGetStat(1);
	int ipStat = ysipGetStat(2);
	int detailStat = ysipGetStat(3);
	array_init(return_value);
	add_next_index_long(return_value, regionStat);
	add_next_index_long(return_value, ipStat);
	add_next_index_long(return_value, detailStat);
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
